import _ from 'lodash';
import { useReducer } from 'react';

/**
 * 定义初始状态信息
 */
export interface IstateType {
  assetList: any; // 选择的资产数据列表
  materialList: any; // 选择的材料数据列表
  addAssetList: any;
  addMaterialList: any;
}

/**
 * 新增和编辑页面操作枚举
 * @CLEAR_RECEIVE  清空存入 数据
 * @SELECT_MATERIEL 选中的材料数据列表
 *
 */
export enum IAcquisitionActionEnum {
  CLEAR_RECEIVE = 'clearAllReceive',
  SELECT_ASSET = 'selectAsset',
  ASSIGN_ASSET = 'assignAsset',
  ASSET_FORM = 'assetForm',
  CANCEL_ASSET = 'cancelAsset',
  SELECT_ADD_ASSET = 'selectAddAsset',

  SELECT_MATERIAL = 'selectMaterial',
  ASSIGN_MATERIAL = 'assignMaterial',
  MATERIAL_FORM = 'materialForm',
  CANCEL_MATERIAL = 'cancelMaterial',
  SELECT_ADD_MATERIAL = 'selectAddMaterial',
}

/**
 * 定义新增和编辑页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IAcquisitionActionEnum;
  payload?: any;
}

export const useAcquisitionManageReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    assetList: [],
    materialList: [],
    addAssetList: [],
    addMaterialList: [],
  };

  const acquisitionManageReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 选中的资产 */
      case IAcquisitionActionEnum.SELECT_ASSET:
        const temp = {
          ...state,
          assetList: action?.payload,
        };
        return temp;
      /** 取消选中的资产列表中的某项 */
      case IAcquisitionActionEnum.CANCEL_ASSET:
        return {
          ...state,
          assetList: action?.payload,
        };
      /** 选中的资产信息 */
      case IAcquisitionActionEnum.ASSET_FORM:
        return {
          ...state,
          assetForm: action?.payload,
        };
      // 自行添加的资产信息
      case IAcquisitionActionEnum.SELECT_ADD_ASSET:
        return {
          ...state,
          addAssetList: action?.payload,
        };

      /** 选中的材料 */
      case IAcquisitionActionEnum.SELECT_MATERIAL:
        return {
          ...state,
          materialList: action?.payload,
        };
      /** 取消选中的材料列表中的某项 */
      case IAcquisitionActionEnum.CANCEL_MATERIAL:
        return {
          ...state,
          materialList: action?.payload,
        };
      /** 选中的资产信息 */
      case IAcquisitionActionEnum.MATERIAL_FORM:
        return {
          ...state,
          materialList: action?.payload,
        };
      // 自行添加的资产信息
      case IAcquisitionActionEnum.SELECT_ADD_MATERIAL:
        return {
          ...state,
          addMaterialList: action?.payload,
        };

      /**
       *  清空已选数据
       */
      case IAcquisitionActionEnum.CLEAR_RECEIVE:
        return {
          ...state,
          leftSelectRows: [],
          leftSelectKeys: [],
          leftExpandedKeys: [],
          leftAutoExpandParent: false,
          leftSelectName: [],
        };

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, acquisitionDispatch] = useReducer(acquisitionManageReducer, initState);

  return {
    ...state,
    acquisitionDispatch,
  };
};
